package com.ebupt.common.base

import android.os.Bundle
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import com.ebupt.common.ktx.bindView

abstract class BaseActivity<ActBinding:ViewDataBinding>:AppCompatActivity() {

    @LayoutRes
    abstract fun getLayoutId():Int

    protected lateinit var viewDataBinding:ActBinding



   open fun initBefore(){

    }

   open fun initData(){

    }

    /**
     * LiveData注册监听
     */
    protected inline fun<T:Any> LiveData<T>.observerKt(crossinline blocak:(T?)->Unit){
        this.observe(this@BaseActivity, Observer {
            blocak.invoke(it)
        })
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        initBefore()
        super.onCreate(savedInstanceState)
        viewDataBinding = bindView(getLayoutId())
        viewDataBinding.apply {
            lifecycleOwner = this@BaseActivity
        }

        initData()
    }


    override fun onDestroy() {
        super.onDestroy()
        if (this::viewDataBinding.isLateinit){
            viewDataBinding.unbind()
        }
    }
}